import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/api/home.dart';
import 'package:flutter_enjoy_plus/components/home_list.dart';
import 'package:flutter_enjoy_plus/components/home_nav.dart';
import 'package:flutter_enjoy_plus/constants/index.dart';
import 'package:flutter_enjoy_plus/utils/RequestDio.dart';
import 'package:flutter_enjoy_plus/utils/Toast.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<HomePage> {
  List notifyList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getAnnounceList();
    tokenManager.initToken();
  }

  // 获取公告数据
  getAnnounceList() async {
    final res = await getAnnounceListAPI();
    PromptAction.showToast('获取公告数据成功');
    print(res);
    setState(() {
      notifyList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(padding: const EdgeInsets.all(10), children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
              onPressed: () {
                tokenManager.setToken('123456789');
                PromptAction.showToast('存储Token成功');
              },
              child: const Text('存储Token'),
            ),
            ElevatedButton(
              onPressed: () {},
              child: const Text('开启'),
            ),
            ElevatedButton(
              onPressed: () {},
              child: const Text('关闭'),
            ),
            ElevatedButton(
              onPressed: () {
                Future.delayed(const Duration(seconds: 3), () {
                  PromptAction.showToast('执行');
                });
              },
              child: const Text('定时器'),
            )
          ],
        ),
        const SizedBox(height: 10),
        // 导航栏
        HomeNav(),
        //  banner图片
        ClipRRect(
          borderRadius: BorderRadius.circular(10),
          child: Image.asset('assets/images/banner@2x.jpg'),
        ),
        //  公告列表
        HomeList(notifyList: notifyList),
      ]),
    );
  }
}
